/**
 * ------------------------------------------------------------------------------------------
 * Copyright (c): Company, 2012 This software is the
 * proprietary information of FHWS, Würzburg All Right Reserved.
 * ------------------------------------------------------------------------------------------
 *
 *
 * @project: Vorlesungplan
 * @copyright FHWS 2012
 * @link       http://mussnocheingeragenwerden.de
 * @package    tt.model
 *
 * SVN revision information:
 *
 * @version $Rev$:
 * @author $Author$:
 * @date $Date$:
 */

package tt.model;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@Table(name = "lecturedate")
@XmlRootElement
@NamedQueries(
{
   @NamedQuery(name = "Lecturedate.findAll", query = "SELECT l FROM Lecturedate l  order by l.day , l.starttime"),
   @NamedQuery(name = "Lecturedate.findByIdlecturedate", query = "SELECT l FROM Lecturedate l WHERE l.idlecturedate = :idlecturedate"),
   @NamedQuery(name = "Lecturedate.findByRoom", query = "SELECT l FROM Lecturedate l WHERE l.room = :room"),
   @NamedQuery(name = "Lecturedate.findByStarttime", query = "SELECT l FROM Lecturedate l WHERE l.starttime = :starttime"),
   @NamedQuery(name = "Lecturedate.findByDay", query = "SELECT l FROM Lecturedate l WHERE l.day = :day"),
   @NamedQuery(name = "Lecturedate.findByTimestamplec", query = "SELECT l FROM Lecturedate l WHERE l.timestamplec = :timestamplec")
})
public class Lecturedate implements Serializable, Comparable<Lecturedate>
{

   private static final long serialVersionUID = 1L;
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Basic(optional = false)
   @Column(name = "idlecturedate")
   private Integer idlecturedate;
   @Basic(optional = false)
   @NotNull
   @Size(min = 1, max = 45)
   @Column(name = "room")
   private String room;
   @Basic(optional = false)
   @NotNull
   @Column(name = "starttime")
   @Temporal(TemporalType.TIME)
   private Date starttime;
   @Basic(optional = false)
   @NotNull
   @Size(min = 1, max = 15)
   @Column(name = "day")
   private String day;

   public Lecturedate(Date starttime)
   {
      this.starttime = starttime;
   }
   @Column(name = "timestamplec")
   @Temporal(TemporalType.TIMESTAMP)
   private Date timestamplec;
   @JoinTable(name = "lecturedate_has_lecturer", joinColumns =
   {
      @JoinColumn(name = "LectureDate_idlecturedate", referencedColumnName = "idlecturedate")
   }, inverseJoinColumns =
   {
      @JoinColumn(name = "lecturer_idlecturer", referencedColumnName = "idlecturer")
   })
   @ManyToMany(cascade = CascadeType.ALL)
   private Collection<Lecturer> lecturerCollection;
   @ManyToMany(mappedBy = "lecturedateCollection")
   private Collection<Courseschedule> coursescheduleCollection;
   @JoinColumn(name = "lecture", referencedColumnName = "idlecture")
   @ManyToOne(optional = false)
   private Lecture lecture;

   public Lecturedate()
   {
   }

   public Lecturedate(Integer idlecturedate)
   {
      this.idlecturedate = idlecturedate;
   }

   public Lecturedate(Integer idlecturedate, String room, Date starttime, String day)
   {
      this.idlecturedate = idlecturedate;
      this.room = room;
      this.starttime = starttime;
      this.day = day;
   }

   public Integer getIdlecturedate()
   {
      return idlecturedate;
   }

   public void setIdlecturedate(Integer idlecturedate)
   {
      this.idlecturedate = idlecturedate;
   }

   public String getRoom()
   {
      return room;
   }

   public void setRoom(String room)
   {
      this.room = room;
   }

   public Date getStarttime()
   {
      return starttime;
   }

   public void setStarttime(Date starttime)
   {
      this.starttime = starttime;
   }

   public String getEndtime()
   {
      String endtime = null;
      //SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

      String time = starttime.toString();
      int minutesToAdd = 90;

      Calendar cal = Calendar.getInstance();
      cal.setTime(starttime);
      cal.add(Calendar.MINUTE, minutesToAdd);
      endtime = cal.getTime().toString();
      endtime = endtime.substring(11, 16) + " Uhr";

      return endtime;
   }

   public String getDay()
   {
      return day;
   }

   public String getDayShort()
   {
       /* 
        * Anfangsbuchstaben der Wochentage für Smartphone Wochenansicht
        */
      String dayShort = day.toString();
      dayShort = dayShort.substring(0, 2);
      return dayShort;
   }

   
   public void setDay(String day)
   {
      this.day = day;
   }

   public Date getTimestamplec()
   {
      return timestamplec;
   }

   public void setTimestamplec(Date timestamplec)
   {
      this.timestamplec = timestamplec;
   }

   @XmlTransient
   public Collection<Lecturer> getLecturerCollection()
   {
      return lecturerCollection;
   }

   public void setLecturerCollection(Collection<Lecturer> lecturerCollection)
   {
      this.lecturerCollection = lecturerCollection;
   }

   @XmlTransient
   public Collection<Courseschedule> getCoursescheduleCollection()
   {
      return coursescheduleCollection;
   }

   public void setCoursescheduleCollection(Collection<Courseschedule> coursescheduleCollection)
   {
      this.coursescheduleCollection = coursescheduleCollection;
   }

   public Lecture getLecture()
   {
      return lecture;
   }

   public void setLecture(Lecture lecture)
   {
      this.lecture = lecture;
   }

   @Override
   public int hashCode()
   {
      int hash = 0;
      hash += (idlecturedate != null ? idlecturedate.hashCode() : 0);
      return hash;
   }

   @Override
   public boolean equals(Object object)
   {
      // TODO: Warning - this method won't work in the case the id fields are not set
      if (!(object instanceof Lecturedate))
      {
         return false;
      }
      Lecturedate other = (Lecturedate) object;
      if ((this.idlecturedate == null && other.idlecturedate != null) || (this.idlecturedate != null && !this.idlecturedate.equals(other.idlecturedate)))
      {
         return false;
      }
      return true;
   }

   @Override
   public String toString()
   {
      return "tt.model.Lecturedate[ idlecturedate=" + idlecturedate + " ]";
   }

   public int compareTo(Lecturedate o)
   {
      int temp = day.compareToIgnoreCase(o.getDay());
      int tempday, tempdayo;

      if (temp == 0)
      {
         return starttime.compareTo(o.getStarttime());
      } else
      {

         //Jeder Wochentag bekommt einen Wert - benötigt fürs sortieren
         if (day.toLowerCase().startsWith("mon"))
         {
            tempday = 1;
         } else if (day.toLowerCase().startsWith("die"))
         {
            tempday = 2;
         } else if (day.toLowerCase().startsWith("mit"))
         {
            tempday = 3;
         } else if (day.toLowerCase().startsWith("don"))
         {
            tempday = 4;
         } else if (day.toLowerCase().startsWith("frei"))
         {
            tempday = 5;
         } else if (day.toLowerCase().startsWith("sam"))
         {
            tempday = 6;
         } else
         {
            tempday = 7;
         }


         if (o.getDay().toLowerCase().startsWith("mon"))
         {
            tempdayo = 1;
         } else if (o.getDay().toLowerCase().startsWith("die"))
         {
            tempdayo = 2;
         } else if (o.getDay().toLowerCase().startsWith("mit"))
         {
            tempdayo = 3;
         } else if (o.getDay().toLowerCase().startsWith("don"))
         {
            tempdayo = 4;
         } else if (o.getDay().toLowerCase().startsWith("frei"))
         {
            tempdayo = 5;
         } else if (o.getDay().toLowerCase().startsWith("sam"))
         {
            tempdayo = 6;
         } else
         {
            tempdayo = 7;
         }

         if (tempday > tempdayo)
         {
            return 11;
         } else
         {
            return -1;
         }
      }//endif



   }//endmethod

   public String getTimestamplecShort()
   {
      String dateDayLong = timestamplec.toString();
      String dateDay = timestamplec.toString();
      String dateMonth = timestamplec.toString();
      String dateYear = timestamplec.toString();
      dateDayLong = dateDayLong.substring(0, 3);
      dateDay = dateDay.substring(8, 10);
      dateMonth = dateMonth.substring(4, 7);
      dateYear = dateYear.substring(25, 29);

      /*
       * Wochentag auf Deutsch und ausgeschrieben darstellen
       */
      if (dateDayLong.compareTo("Mon") == 0)
      {
         dateDayLong = "Montag, ";
      } else if (dateDayLong.compareTo("Tue") == 0)
      {
         dateDayLong = "Dienstag, ";
      } else if (dateDayLong.compareTo("Wed") == 0)
      {
         dateDayLong = "Mittwoch, ";
      } else if (dateDayLong.compareTo("Thu") == 0)
      {
         dateDayLong = "Donnerstag, ";
      } else if (dateDayLong.compareTo("Fri") == 0)
      {
         dateDayLong = "Freitag, ";
      } else if (dateDayLong.compareTo("Sat") == 0)
      {
         dateDayLong = "Samstag, ";
      }

      /*
       * Monat Aug in Dezimalzahl 08 umwandeln
       */
      if (dateMonth.compareTo("Jan") == 0)
      {
         dateMonth = ".01.";
      } else if (dateMonth.compareTo("Feb") == 0)
      {
         dateMonth = ".02.";
      } else if (dateMonth.compareTo("Mar") == 0)
      {
         dateMonth = ".03.";
      } else if (dateMonth.compareTo("Apr") == 0)
      {
         dateMonth = ".04.";
      } else if (dateMonth.compareTo("May") == 0)
      {
         dateMonth = ".05.";
      } else if (dateMonth.compareTo("Jun") == 0)
      {
         dateMonth = ".06.";
      } else if (dateMonth.compareTo("Jul") == 0)
      {
         dateMonth = ".07.";
      } else if (dateMonth.compareTo("Aug") == 0)
      {
         dateMonth = ".08.";
      } else if (dateMonth.compareTo("Sep") == 0)
      {
         dateMonth = ".09.";
      } else if (dateMonth.compareTo("Oct") == 0)
      {
         dateMonth = ".10.";
      } else if (dateMonth.compareTo("Nov") == 0)
      {
         dateMonth = ".11.";
      } else if (dateMonth.compareTo("Dec") == 0)
      {
         dateMonth = ".12.";
      }
     return dateDay + dateMonth + dateYear;
     // return dateDayLong + dateDay + dateMonth + dateYear;
      //Thu Jan 01 11:45:00 CET 1970 => Donnerstag, 01.01.1970

   }//end method

   public String getStarttimeShort()
   {
      String time = starttime.toString();
      time = time.substring(11, 16) + " -";
      return time;
      //Thu Jan 01 11:45:00 CET 1970

   }//end method
   
    public String getStarttimeShortSmartphone()
   {
      String time = starttime.toString();
      time = time.substring(11, 16);
      return time;
      //Thu Jan 01 11:45:00 CET 1970

   }//end method

   public int getHourofStarttime()
   {
      int hour;
      String time = starttime.toString();
      time = time.substring(11, 13);
      hour = Integer.parseInt(time);
      return hour;
      //Thu Jan 01 11:45:00 CET 1970
   }//endmethod

   public int getMinuteofStarttime()
   {
      int minute;
      String time = starttime.toString();
      time = time.substring(14, 16);
      minute = Integer.parseInt(time);
      return minute;
      //Thu Jan 01 11:45:00 CET 1970
   }//endmethod

   public Lecturedate getCopy()
   {
      Lecturedate my = new Lecturedate();
      my.setDay(day);
      my.setIdlecturedate(idlecturedate);
      my.setRoom(room);
      my.setStarttime(starttime);
      // my.setCoursescheduleCollection(coursescheduleCollection);
      // my.setLecturerCollection(lecturerCollection);
      // my.setLecture(lecture);
      return my;
   }//endmehtod

   public boolean isUpdate()
   {
      long isNew;
      Date lastDate = getTimestamplec();
      Date curDate = new Date();
      long lastDate_long = lastDate.getTime();
      long curDate_long = curDate.getTime();
      isNew = curDate.getTime() - lastDate.getTime();

      if (isNew < 0)
      {
         isNew = isNew * (-1);
      }//endif

      //Wert "12096e5" = 2 Wochen in Millisekunden
      if (isNew <= 12096e5)
      {
         return true;
      } else
      {
         return false;
      }//endif
   }//endmethod
}//endclass